package com.jcroke.DACGraph;

import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.jcroke.DACGraph.DAC.DACClient;

/**
 * @author jcroke
 * 
 */

public class Logic {
	private RDFNode _rdfNode;
	private RDFNode _sourceRDFValue;
	private RDFNode _sourceRDFLogic;

	private RDFNode _sourceRDFName;
	private RDFNode _sourceRDFClass;
	private RDFNode _sourceRDFCode;
	private RDFNode _sourceRDFImport;
	private RDFNode _sourceRDFThrows;

	public static final String LOGIC = "logic";
	public static final String NAME = "name";
	public static final String VALUE = "value";
	public static final String CLASS = "class";

	// code injection
	public static final String CODE = "code";
	public static final String IMPORT = "import";
	public static final String THROW = "throw";

	public Logic(DACClient dac, QuerySolution qs) {
		ResultSet resultSet;
		this._rdfNode = qs.get(NAME);
		this._sourceRDFValue = qs.get(VALUE); // Indicates source, but is for now local.
		this._sourceRDFLogic = qs.get(LOGIC);

		if (this._sourceRDFLogic != null) {

			// Get the logic classes
			resultSet = dac.sparql(String.format(
					SPARQL.CLASS_LOGIC_QUERY_TEMPLATE,
					this._sourceRDFLogic.toString()));

			if (resultSet.hasNext()) {
				QuerySolution ls = resultSet.next();
				this._sourceRDFName = ls.get(NAME);
				this._sourceRDFClass = ls.get(CLASS);

				// code injection
				this._sourceRDFImport = ls.get(IMPORT);
				this._sourceRDFThrows = ls.get(THROW);
				this._sourceRDFCode = ls.get(CODE);
			}
		}
	}

	public String getIRI() {
		return (_rdfNode != null ? _rdfNode.toString() : null);
	}

	public RDFNode getSourceRDFValue() {
		return _sourceRDFValue;
	}

	public RDFNode getSourceRDFLogic() {
		return _sourceRDFLogic;
	}

	public RDFNode getSourceRDFName() {
		return _sourceRDFName;
	}

	public RDFNode getSourceRDFClass() {
		return _sourceRDFClass;
	}

	public RDFNode getSourceRDFCode() {
		return _sourceRDFCode;
	}

	public RDFNode getSourceRDFImport() {
		return (_sourceRDFImport);
	}
	
	public boolean hasThrows() {
		return (_sourceRDFThrows != null);
	}
	
	public RDFNode getSourceRDFThrows() {
		return (_sourceRDFThrows);
	}

	public boolean hasLinkedLogic() {
		return (_sourceRDFCode != null || _sourceRDFClass != null || _sourceRDFValue != null);
	}

	public boolean hasCode() {
		return (_sourceRDFCode != null);
	}

	public boolean hasClass() {
		return (_sourceRDFClass != null);
	}

	public boolean hasValue() {
		return (_sourceRDFValue != null);
	}

	public boolean hasImport() {
		return (_sourceRDFImport != null);
	}
	
}
